﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace SodiumHydroxide
{
    public partial class ArticleView : Form
    {
        PublishingCompanyEntities publishContext;
        List<Article> articleList;
        int currentArticleIndex = 0;

        public ArticleView()
        {
            InitializeComponent();
        }

        private void ArticleView_Load(object sender, EventArgs e)
        {
            publishContext = new PublishingCompanyEntities();
            articleList = new List<Article>();


            //ObjectResult<Article> articleQuery = publishContext.GetArticle(1);
            //currentArticle = articleQuery.ToList().First();
            //PopulateFields();


            //ObjectResult<Article> articleQuery = publishContext.GetArticles();
            IEnumerable<Article> articleQuery = from ar in publishContext.GetArticles()
                                                select ar;

            articleList = articleQuery.ToList();
            
            PopulateFields();


        }

        private void PopulateFields()
        {

            Article currentArticle = articleList[currentArticleIndex];
            currentArticle.AuthorReference.Load();
            articleIDLabel.Text = currentArticle.ArticleID.ToString();
            titleText.Text = currentArticle.Title;
            bodyText.Text = currentArticle.Body;
        }

        private void ArticleView_FormClosing(object sender, FormClosingEventArgs e)
        {
            publishContext.Dispose();
        }

        private void firstButton_Click(object sender, EventArgs e)
        {
            currentArticleIndex = 0;
            PopulateFields();
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            if (currentArticleIndex > 0)
            {
                currentArticleIndex -= 1;
                PopulateFields();
            }
            else
            {
                MessageBox.Show("No more articles to display");
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (currentArticleIndex == articleList.Count - 1)
            {
                MessageBox.Show("No more articles to display");
            }
            else
            {
                currentArticleIndex += 1;
                PopulateFields();
            }
        }

        private void lastButton_Click(object sender, EventArgs e)
        {
            currentArticleIndex = articleList.Count - 1;
            PopulateFields();
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            Article currentArticle = articleList[currentArticleIndex];
            currentArticle.Title = titleText.Text;
            currentArticle.Body = bodyText.Text;

        }

        private void clearForNewButton_Click(object sender, EventArgs e)
        {
            articleIDLabel.Text = "-1";
            titleText.Text = string.Empty;
            bodyText.Text = string.Empty;
        }

        private void saveAsNew_Click(object sender, EventArgs e)
        {
            Article newArticle = new Article();
            newArticle.Title = titleText.Text;
            newArticle.Body = bodyText.Text;
            newArticle.ArticleID = -1;
            publishContext.AddToArticle(newArticle);
            articleList.Add(newArticle);
            currentArticleIndex = articleList.Count - 1;
            PopulateFields();
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            Article currentArticle = articleList[currentArticleIndex];
            publishContext.DeleteObject(currentArticle);
            articleList.Remove(currentArticle);
            currentArticleIndex = 0;
            PopulateFields();
        }

        private void submitToDatabase_Click(object sender, EventArgs e)
        {
            publishContext.SaveChanges();
            PopulateFields();
        }







    }
}
